{extend name="shared/layout" /}
{block name="title"}修改小程序{/block}
{block name="css"}
    <style>
        #btn-upfile{position:relative;overflow:hidden;cursor:pointer;}
        #file{position:absolute;right:0;top:0;width:100px;height:100px;opacity:0; ilter:alpha(opacity=0);cursor:pointer;}
    </style>
{/block}
{block name="javascript"}
    <script type="text/javascript">
         $(document).ready(function () {
            $("#ok").bind("click", Save);
            $("#cancel").bind("click", function () {
                parent.layer.close(parent.layer_edit_index);
            });

            var fileEls = document.getElementById('file');
            var upFileBtnEls = document.getElementById('btn-upfile');
            var fullBase64;
            var base64;
            fileEls.onchange = function(evt){
                if (!window.FileReader) return;
                var files = evt.target.files;
                for (var i = 0, f; f = files[i]; i++) {  
                    if (!f.type.match('image.*')) {  
                        continue;  
                    }

                    var reader = new FileReader();
                    reader.onload = (function(theFile) {
                        return function(e) {
                            document.getElementById('previewImage').src = e.target.result;
                            fullBase64 = e.target.result;
                            appendFile(fullBase64);
                        };
                    })(f);
                    reader.readAsDataURL(f);
                }
            };            
         })

         function appendFile(entry){
            var img = new Image();
            img.src = entry;
            
            img.onload = function () {
                var that = this;
                //设置图片宽高

                var w = 90;
                var h = 65;
        
                //生成canvas
                var canvas = document.createElement('canvas');
                var ctx = canvas.getContext('2d');
                $(canvas).attr({width : w, height : h});
                ctx.drawImage(that, 0, 0, w, h);
                base64 = canvas.toDataURL('image/jpeg', 0.9);   //1z 表示图片质量，越低越模糊。
                var up_img = document.getElementById('up_img');
                up_img.value = entry;
            }
        }

         function Save() {
            layui.use(['layer'], function(){
                var jumpname = $.trim($("input[name=jump_name]").val());
                if (jumpname.length == 0) {                
                    layer.tips('跳转名称不能为空！', 'input[name=jump_name]', { tips: [2, '#78BA32'] });
                    return false;
                }
            
                var appid = $.trim($("input[name=appid]").val());
                if (appid.length == 0) {                
                    layer.tips('目标小程序appid不能为空！', 'input[name=appid]', { tips: [2, '#78BA32'] });
                    return false;
                }

                var path = $.trim($("input[name=path]").val());
                if (path.length == 0) {                
                    layer.tips('目标小程序路径不能为空！', 'input[name=path]', { tips: [2, '#78BA32'] });
                    return false;
                }        
            
                var load_index = layer.load(0, { shade: false });
                $.ajax({
                    type: "post",
                    url: "{:url('singlepage/editjump')}?t=" + new Date(),
                    data: $("form").serialize(),
                    dataType: "json",
                    success: function (result) {
                        layer.close(load_index);
                        if (result.code == 0) {
                            layer.alert('修改成功！', function () {
                                if (typeof (parent.initTable) == "function")
                                    parent.initTable();
                                parent.layer.close(parent.layer_edit_index)
                            });
                        } else {
                            layer.alert(result.msg);
                        }
                    }
                });
            })
        }
    </script>
{/block}
{block name="content"}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <form method="post" id="fmNew" class="form-horizontal">
                            <input type="hidden" name="base64pic" id="up_img" />
                            <input type="hidden" name="id" value="{$data.id}" />
                            <div class="form-group">
                                <label class="col-sm-2 control-label">名称：</label>
                                <div class="col-sm-8">
                                    <input type="text" id="txtjump_name" name="jump_name" value="{$data.jump_name}" class="form-control" placeholder="跳转名称">
                                </div>
                            </div>                                    
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">小程序appid：</label>
                                <div class="col-sm-8">
                                    <input type="text" id="txtappid" name="appid" value="{$data.appid}" class="form-control" placeholder="跳转目标小程序appid">
                                </div>
                            </div>                                    
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">跳转路径：</label>
                                <div class="col-sm-8">
                                    <input type="text" id="txtpath" name="path" value="{$data.path}" class="form-control" placeholder="跳转目标小程序路径">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">封面图片：</label>
                                <div class="col-sm-6">
                                    <input type="text" name="cover_url" id="txtcover_url" value="{$data.cover_url}" class="form-control" placeholder="封面图片地址(上传或者直接填写外链)">                                    
                                </div>                                
                                <div class="col-sm-1">
                                    <span id="btn-upfile" class="btn btn-warning btn-sm" style="margin-bottom:0;padding:6px 8px;">
                                        <i class="glyphicon glyphicon-folder-open" style="padding-right:3px;"></i> 选择上传<input id="file" type="file" name="img">
                                    </span>
                                </div>
                                <div class="col-sm-1">
                                    <img id="previewImage" style="height:34px;">
                                </div>
                            </div>                        
                        </div>
                        <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-sm-12 col-sm-offset-5">
                                    <button class="btn btn-primary" id="ok" type="button">提交</button>
                                    <button class="btn btn-white" id="cancel" type="button">取消</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
{/block}